package com.ibeeking.found.file.rest.service.impl;

import com.ibeeking.found.common.utils.UrlUtils;
import com.ibeeking.found.file.rest.service.IOssFileService;
import com.ibeeking.nematos.file.oss.config.OssProp;
import com.ibeeking.nematos.log.utils.LogUtils;

import com.ibeeking.nematos.utils.file.OSSUtils;
import com.aliyun.oss.OSS;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * @ClassName OssFileServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-06-24 19:23
 **/
@Service
public class OssFileServiceImpl implements IOssFileService {

    @Resource
    private OSS ossClient;

    @Resource
    private OssProp ossProp;

    @Value("${ibeeking.file.oss.host}")
    private String ossHost;

    @Override
    public void queryFile(String fileName, HttpServletResponse response) {
        LogUtils.debug("查询文件：{}",fileName);
        OSSUtils.downLoad(ossClient, ossProp.getBucket(), fileName, response);

    }

    @Override
    public String download(String fileName, String  localFilePath) {
        LogUtils.debug("下载文件{}到本地{}",fileName, localFilePath);
        return OSSUtils.downLoad(ossClient, ossProp.getBucket(), fileName, localFilePath);
    }

    @Override
    public void download(String fileName, HttpServletResponse response) {
        LogUtils.debug("下载文件{}",fileName);
        OSSUtils.downLoad(ossClient, ossProp.getBucket(), fileName, response);
    }

    @Override
    public String upload(String fullFilePath, InputStream inputStream) {

        LogUtils.debug("上传文件{}",fullFilePath);
        String resultKey = OSSUtils.streamUpload(ossClient, inputStream, ossProp.getBucket(), fullFilePath);
        return UrlUtils.addUrlDomainName(ossHost, resultKey);
    }
}
